package com.ele.Servlet;

import cn.hutool.core.util.StrUtil;
import com.ele.Pojo.Business;
import com.ele.Service.BusinessService;
import com.ele.Service.impl.BusinessServiceImpl;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/businessLogin")

public class BusinessLoginServlet extends HttpServlet {
    //service对象
    BusinessServiceImpl businessService = new BusinessServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String account =req.getParameter("account");
        String password =req.getParameter("password");
        //校验
        //先判断传入的是否为空
        if(StrUtil.isBlankIfStr(account)||StrUtil.isBlankIfStr(password)){
            // 这里可以返回不同的标记，前端根据标记给出对应的提示信息）
            // 0 ： 表示账号或密码为空
            // 1 ： 表示账号或密码错误
            // 2 ： 表示登录成功
            resp.getWriter().write(0+"");
            return ;
        }
        Business business =businessService.businessLogin(account,password);

        if(business==null){
            resp.getWriter().write(1+"");

        }else {
            //登陆成功将对象传给前端
            resp.setContentType("application/json;charset=utf-8");
            Gson gson = new Gson();
            String json =gson.toJson(business);
            resp.getWriter().write(json);
        }

    }

}
